package com.df.springmvc.common.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.df.springmvc.common.bean.MsgBean;
import com.df.springmvc.common.services.FileServices;
import com.df.springmvc.common.util.WebUtil;
import com.df.springmvc.supper.SupperController;

@Controller
@RequestMapping("/common/File")
/**
 * 本类仅做一些简单数据处理如:
 *格式验证
 *数据封装
 * @author David
 *
 */
public class FileController extends SupperController{
	String page_model="common/" + this.getClass().getSimpleName().replace("Controller","") + "_";
	@Autowired
	FileServices services;
	@Autowired
	MessageSource messageSource;
	
	public FileController(){
		// TODO Auto-generated constructor stub
	}
	@Override
	@RequestMapping("/toAdd")
	public String toAdd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		HashMap<String,String> paramMap=getParamMap(request,"");
		try{
			ArrayList<HashMap<String,Object>> list=services.getFileList(paramMap);
			request.setAttribute("fileList",list);
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return page_model + super.toAdd(model,request,response);
	}
	@Override
	@RequestMapping("/doDel")
	public void doDel(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			HashMap<String,String> paramMap=getParamMap(request,"map");
			Object rs=services.del(paramMap);
			WebUtil.json(response,new MsgBean(rs,true));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
		super.doDel(model,request,response);
	}
	@Override
	@RequestMapping("/toView")
	public String toView(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		String fileId=getParam(request,"fileId");
		request.setAttribute("fileId",fileId);
		return page_model + super.toView(model,request,response);
	}
	@RequestMapping("/doAdd")
	public void doAdd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			services.upload(request,getParamMap(request,"map"));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.doAdd(model,request,response);
	}
	/**
	 * 上传文件到文件夹
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/doAdd1")
    public @ResponseBody Map upload1(HttpServletRequest request, HttpServletResponse response) {
		Map map = services.upload1(request, response, getParamMap(request,"map"));
		return map;
    }
	@RequestMapping("/doDownLoad")
	public void doDownLoad(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			HashMap<String,String> paramMap=getParamMap(request,"");
			Object rs=services.image(request,response,paramMap);;
			WebUtil.image(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
		super.doXXX(model,request,response);
	}
	@RequestMapping("/doConfirm")
	public void doConfirm(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			services.doConfirm(request,getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean("ok"));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
		super.doAdd(model,request,response);
	}
	@RequestMapping("/getFileList")
	public void getFileList(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		HashMap<String,String> paramMap=getParamMap(request,"map");
		WebUtil.json(response,new MsgBean(session.getAttribute(paramMap.get("fileGroupId"))));
		super.doView(model,request,response);
	}
}